package com.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class WbsGainLossParseXML {
	static List<String> sqlFieldName = new ArrayList<String>();//数据库中表的字段名 带下划线 _
	static Map<String, String> nameCommentMap = new HashMap<String, String>();

	static {
		getFieldCommentFromSql("src/tbl_payment_real_base.sql");
	}
	public static void main(String[] args) {
		// System.out.println(getFieldNamesFromModel(WtradeCashAcrlNew.class).size());
		// System.out.println(getFieldTypesFromSql("src/newfile.sql").size());
//		getFieldCommentFromSql("src/tbl_payment_real_base.sql");

		read(getFieldNamesFromModel(PaymentRealBase.class)//
				, getFieldTypesFromSql("src/tbl_payment_real_base.sql")//
				, nameCommentMap);

		System.out.println("执行完毕！");
	}

	/**
	 * 获取字段类型 从自己创建的sql文件提取
	 * 
	 * @param fileName
	 * @return
	 */
	private static List<String> getFieldTypesFromSql(String fileName) {
		File f = new File(fileName);
		List<String> names = new ArrayList<String>();
		try {
			LineNumberReader lineNumberReader = new LineNumberReader(new FileReader(f));

			String lineStr = lineNumberReader.readLine();
			int allIndex = 0;
			String regex = "\\s+";
			boolean key = false;// 开始抓数据的锁,开始时关闭的
			while (lineStr != null) {
				if (lineStr.indexOf("create table") != -1 || key) {
					key = true;// 锁打开
					if (lineStr.indexOf(");") != -1) {
						break;
					}
					if (allIndex > 1) {
						String[] lineS = lineStr.split(regex);
						for (int i = 0; i < lineS.length; i++) {
							if (i == 1) {
								sqlFieldName.add(lineS[i]);
							}
							if (i == 2) {
								String s = lineS[i];
								if (lineS[i].indexOf(',') != -1) {
									s = s.substring(0, lineS[i].indexOf(','));
								}
								names.add(s);
							}
						}
						System.out.println();
					}
					lineStr = lineNumberReader.readLine();
					allIndex++;
				} else {
					lineStr = lineNumberReader.readLine();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println(names);
		return names;
	}

	/**
	 * 获取字段名称 从model文件提取
	 * 
	 * @param classType
	 * @return
	 */
	private static List<String> getFieldNamesFromModel(Class<?> classType) {
		// 获得对象的类型
		System.out.println("Class:" + classType.getName());
		List<String> names = new ArrayList<String>();
		// 获得对象的所有属性
		Field fields[] = classType.getDeclaredFields();

		for (int i = 0; i < fields.length; i++) {
			Field field = fields[i];

			String fieldName = field.getName();
			names.add(fieldName);
		}
		return names;
	}

	/**
	 * 获取字段的注释 从sql文件提取
	 * 
	 * @param fileName
	 * @return
	 */
	private static Map<String, String> getFieldCommentFromSql(String fileName) {
		getFieldTypesFromSql("src/tbl_payment_real_base.sql").size();
		Map<String, String> names = new HashMap<String, String>();
		try {
			LineNumberReader lineNumberReader = new LineNumberReader(new InputStreamReader(new FileInputStream(fileName), "gb2312"));
			String lineStr = lineNumberReader.readLine();
			while (lineStr != null) {
				if (lineStr.indexOf("column") == -1) {
					lineStr = lineNumberReader.readLine();
					continue;
				}
				for (String name : sqlFieldName) {
					String myName = new String(name);//不要在for循环中修改集合数据,要做一个备份
					
					if (lineStr.indexOf(myName) != -1) {// 这一行有该字段的评注
						int indexFind = lineStr.indexOf(myName);
						if (lineStr.charAt(indexFind + myName.length()) == '_' || (65 <= lineStr.charAt(indexFind + myName.length()) && lineStr.charAt(indexFind + myName.length()) <= 90)) {//如果找到该词的后面是一个"_",说明还是没有找对,返回继续找
							continue;
						}
						if (lineStr.charAt(indexFind - 1) == '_' || (65 <= lineStr.charAt(indexFind - 1) && lineStr.charAt(indexFind - 1) <= 90)) {//如果找到该词的前面是一个"_",说明还是没有找对,返回继续找
							continue;
						}
						Pattern p = Pattern.compile("[\u4e00-\u9fa5]+");
						Matcher m = p.matcher(lineStr);
						if (m.find()) {
							// names.add(m.group() + "\n");
							names.put(myName, m.group());
							
							if (myName.indexOf('_') != -1) {
								while(myName.indexOf('_') != -1){
									int index = myName.indexOf('_');
									String afterIndexChar = String.valueOf(myName.charAt(index + 1)).toUpperCase();
									myName = myName.replaceFirst("_\\w", String.valueOf(afterIndexChar));
								}
								nameCommentMap.put(myName, m.group());
							}else{
								nameCommentMap.put(myName, m.group());
							}
							
						}
					}
				}
				lineStr = lineNumberReader.readLine();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println(names);
		System.out.println(nameCommentMap);
		return names;
	}

	/**
	 * 生成总得结构字符串
	 * 
	 * @param first
	 * @param second
	 */
	public static void read(List<String> name, List<String> type, Map<String, String> comment) {

		StringBuilder sb = new StringBuilder();
		int i = 0;
		int L1 = 100;
		int L2 = 100;
		int L3 = 100;
		String commentssss = "";
		for (int j2 = 0; j2 < type.size(); j2++) {
			for (String namesss : comment.keySet()) {
				if (name.get(j2).equals(namesss)) {
					commentssss = comment.get(namesss);
					break;
				} 
			}
			if (commentssss.equals("")) {
				commentssss = "暂时缺少评注";	
			}
			if (i == 0) {
				sb.append("");
				sb.append("\"" + (L1++) + "__11017__" + name.get(j2) + "__" + type.get(j2) + "__" + commentssss + "\\r\" +\n");
				System.out.println(commentssss);
				commentssss = "";
			} else if (i == 1) {
				sb.append("\"" + (L2++) + "__58__" + name.get(j2) + "__" + type.get(j2) + "__" + commentssss + "\\r\" +\n");
				System.out.println(commentssss);
				commentssss = "";
			} else if (i == 2) {
				sb.append("\"" + (L3++) + "__10994__" + name.get(j2) + "__" + type.get(j2) + "__" + commentssss + "\\r\" +\n\n");
				i = -1;
				System.out.println(commentssss);
				commentssss = "";
			}
			i++;
		}
		int lastAddRef = sb.lastIndexOf("+");
		sb.replace(lastAddRef, lastAddRef + 1, ";");
		System.out.println(sb);
	}
}